<?php
class Model_User extends ORM
{
//    protected $_ignored_columns = array('password_confirm');
//    protected $_table_name = 'strange_tablename';
//    protected $_primary_key = 'strange_pkey';
//    protected $_db = 'alternate';

    protected $has_and_belongs_to_many = array('roles');

    protected $_has_many = array(
        'user_tokens' => array('model' => 'user_token'),
        'roles'       => array('model' => 'role', 'through' => 'roles_users'),
    );

    public function unique_key($id = NULL)
    {
        if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id) ) {
            return 'username';
        }

        return parent::unique_key($id);
    }

    /**
     * Vervollständigt den Login und erhöht die Logins um 1, setzt Login Zeit
     *
     * @return void
     */
    public function complete_login()
    {
        if ($this->_loaded)
        {
            // Update the number of logins
            $this->logins = new Database_Expression('logins + 1');

            // Set the last login date
            $this->last_login = time();

            // Save the user
            $this->update();
        }
    }

    public function getUser($userId)
    {
        return $this->where('id', '=', (int) $userId)->find();
    }

}